package com.eb.task;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.eb.constant.Constant;
import com.eb.constant.RedisConstant;
import com.eb.domain.*;
import com.eb.mapper.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.util.List;

@Component
@Slf4j
/**
 * 排行榜任务类
 */
public class RankTask {

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @Autowired
    IInnovationMapper iInnovationMapper;
    @Autowired
    IUserMapper iUserMapper;
    @Autowired
    IPreserveMapper iPreserveMapper;
    @Autowired
    ILikesMapper iLikesMapper;
    @Autowired
    ICommentMapper iCommentMapper;
    @Autowired
    ILookMapper iLookMapper;

    /**
     * 定时任务，启动时立即执行，每过5分钟执行一次
     */
    @Scheduled(cron = "0 0/5 * * * ? ")
    @PostConstruct
    public void updateRank(){
        log.info("定时任务执行,刷新排行榜");
        List<Innovation> innovations = iInnovationMapper.selectList(
                new QueryWrapper<Innovation>().select("id","user_id")
        );
        //遍历帖子，查找出点赞量，评论量，收藏量，浏览量
        for (Innovation innovation:innovations){
            Integer innovationId = innovation.getId();
            Long likenum = iLikesMapper.selectCount(
                    new QueryWrapper<Likes>().eq("innovation_id",innovationId)
            );
            Long preservenum = iPreserveMapper.selectCount(
                    new QueryWrapper<Preserve>().eq("innovation_id",innovationId)
            );
            Long commentnum = iCommentMapper.selectCount(
                    new QueryWrapper<Comment>().eq("innovation_id",innovationId)
            );
            Long looknum = iLookMapper.selectCount(
                    new QueryWrapper<Look>().eq("innovation_id",innovationId)
            );
            Integer userlevel = iUserMapper.selectById(innovation.getUserId()).getLevel();

            Double hot = calculateHot(likenum, preservenum, commentnum, looknum, userlevel);
            stringRedisTemplate.opsForZSet().add("innovation.rank",
                    RedisConstant.INNOVATION_RANK+innovationId.toString(),hot);
        }
        log.info("排行榜刷新完成");
    }
    //计算热度
    private Double calculateHot(Long likenum,Long preservenum,Long commentnum,
                              Long looknum,Integer userlevel){
        if (looknum != 0)
        {
            Double part1= likenum * Constant.LIKE_WEIGHT * likenum/looknum*1.0
                  +preservenum * Constant.PRESERVE_WEIGHT * preservenum/looknum*1.0
                    +commentnum * Constant.COMMENT_WEIGHT+looknum*Constant.LOOK_WEIGHT;
            Double part2 = (Math.log(userlevel)/Math.log(2))+1;//对数换底公式计算log2 Level
            return part2*part1;
        }else
        {
            return 0.0;
        }


    }
}
